package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.enums.CommonStatusEnum;
import com.dimples.dd.common.model.Option;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.DictForm;
import com.dimples.dd.system.model.form.DictTypeForm;
import com.dimples.dd.system.model.query.DictPageQuery;
import com.dimples.dd.system.model.query.DictTypePageQuery;
import com.dimples.dd.system.model.vo.DictDataSimpleRespVO;
import com.dimples.dd.system.model.vo.DictPageVO;
import com.dimples.dd.system.model.vo.DictTypePageVO;
import com.dimples.dd.system.service.SystemDictDataService;
import com.dimples.dd.system.service.SystemDictTypeService;
import com.dimples.dd.web.core.annotation.PreventDuplicateResubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "05.字典接口")
@RestController
@Validated
@RequestMapping("dict")
@RequiredArgsConstructor
public class SysDictController {

    private final SystemDictDataService dictService;

    private final SystemDictTypeService dictTypeService;

    @Operation(summary = "字典分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:dict-type:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<DictPageVO> selectDictPage(
            @ParameterObject @Valid DictPageQuery queryParams
    ) {
        return this.dictService.selectDictPage(queryParams);
    }

    @Operation(summary = "字典数据表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:dict:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<DictForm> selectDictForm(
            @Parameter(description = "字典ID") @PathVariable Long id
    ) {
        DictForm formData = this.dictService.selectDictForm(id);
        return CommonResult.success(formData);
    }

    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @Operation(summary = "获得全部字典数据列表", description = "一般用于管理后台缓存字典数据在本地")
    // 无需添加权限认证，因为前端全局都需要
    public CommonResult<List<DictDataSimpleRespVO>> selectSimpleDictDataList() {
        List<DictDataSimpleRespVO> list = this.dictService.selectDictDataList(CommonStatusEnum.ENABLE.getValue(), null);
        return CommonResult.success(list);
    }

    @Operation(summary = "新增字典")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:dict:create')")
    @PreventDuplicateResubmit
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> createDict(
            @RequestBody DictForm DictForm
    ) {
        boolean result = this.dictService.createDict(DictForm);
        return CommonResult.success(result);
    }

    @Operation(summary = "修改字典")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('system:dict:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateDict(
            @PathVariable Long id,
            @RequestBody DictForm DictForm
    ) {
        boolean status = this.dictService.updateDict(id, DictForm);
        return CommonResult.success(status);
    }

    @Operation(summary = "删除字典")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:dict:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteDict(
            @Parameter(description = "字典ID，多个以英文逗号(,)拼接") @NotBlank(message = "不能为空") @PathVariable String ids
    ) {
        boolean result = this.dictService.deleteDict(ids);
        return CommonResult.success(result);
    }


    @Operation(summary = "字典下拉列表")
    @GetMapping("/options")
    @PreAuthorize("@ss.hasPerm('system:dict:query')")
    public CommonResult<List<Option<String>>> listDictOptions(
            @Parameter(description = "字典类型编码") @RequestParam String typeCode
    ) {
        List<Option<String>> list = this.dictService.listDictOptions(typeCode);
        return CommonResult.success(list);
    }


    /*----------------------------------------------------*/
    @Operation(summary = "字典类型分页列表")
    @GetMapping("/types/page")
    @PreAuthorize("@ss.hasPerm('system:dict-type:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<DictTypePageVO> selectDictTypePage(
            @ParameterObject DictTypePageQuery queryParams
    ) {
        return this.dictTypeService.selectDictTypePage(queryParams);
    }

    @Operation(summary = "字典类型表单数据")
    @GetMapping("/types/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:dict-type:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<DictTypeForm> selectDictTypeForm(
            @Parameter(description = "字典ID") @PathVariable Long id
    ) {
        DictTypeForm dictTypeForm = this.dictTypeService.selectDictTypeForm(id);
        return CommonResult.success(dictTypeForm);
    }

    @Operation(summary = "新增字典类型")
    @PostMapping("/types")
    @PreAuthorize("@ss.hasPerm('system:dict-type:create')")
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    @PreventDuplicateResubmit
    public CommonResult<Boolean> createDictType(@RequestBody @Valid DictTypeForm dictTypeForm) {
        boolean result = this.dictTypeService.createDictType(dictTypeForm);
        return CommonResult.success(result);
    }

    @Operation(summary = "修改字典类型")
    @PutMapping("/types/{id}")
    @PreAuthorize("@ss.hasPerm('system:dict-type:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateDictType(@PathVariable Long id, @RequestBody @Valid DictTypeForm dictTypeForm) {
        boolean status = this.dictTypeService.updateDictType(id, dictTypeForm);
        return CommonResult.success(status);
    }

    @Operation(summary = "删除字典类型")
    @DeleteMapping("/types/{ids}")
    @PreAuthorize("@ss.hasPerm('system:dict-type:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteDictTypes(
            @Parameter(description = "字典类型ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.dictTypeService.deleteDictTypes(ids);
        return CommonResult.success(result);
    }

    @Operation(summary = "获取字典类型的数据项")
    @GetMapping("/types/{typeCode}/items")
    @PreAuthorize("@ss.hasPerm('system:dict-type:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<List<Option<Long>>> listDictTypeItems(
            @Parameter(description = "字典类型编码") @PathVariable String typeCode
    ) {
        List<Option<Long>> list = this.dictTypeService.listDictItemsByTypeCode(typeCode);
        return CommonResult.success(list);
    }


}
